//
//  DefrayViewController.m
//  KD
//
//  Created by Hubery on 15/8/19.
//  Copyright (c) 2015年 centling. All rights reserved.
//

#import "DefrayViewController.h"
#import "HeaderView.h"
#import "AddressGroupModel.h"
#import "DefraySucceedViewController.h"

@interface DefrayViewController ()<HeaderViewDelegate>
/**
 假数据
 */
@property(nonatomic,strong) NSArray *array;//保存组模型
@property(nonatomic,strong) NSArray *myArray;//保存假数据模型
@property(nonatomic,strong) NSArray *invoices;//发票
@property(nonatomic,strong) NSArray *addresses;//地址
@property(nonatomic,strong) NSArray *accounts;//账号
@property(nonatomic,weak) UIButton *accessoryBtn;
@property(nonatomic,strong) UIView *xxview;
@property(nonatomic,strong) UIImageView *arrowBtn;
@property(nonatomic,strong)NSMutableArray *nameArray;
@end

@implementation DefrayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

+ (instancetype)initWithStoryBoard{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    return [sb instantiateViewControllerWithIdentifier:@"DefrayViewController"];
}


#pragma mark - TableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
        {
            AddressGroupModel *add = [self.array objectAtIndex:0];
            if (add.open) {
                return add.addresses.count+1;
            }else
            {
                return 0;
            }
        }
            break;

        case 1:
        {
            AddressGroupModel *add = [self.array objectAtIndex:1];
            if (add.open) {
                return add.addresses.count+1;
            }else
            {
                return 0;
            }
        }
            break;
            
        case 2:
        {
            AddressGroupModel *add = [self.array objectAtIndex:2];
            if (add.open) {
                return add.addresses.count+1;
            }else
            {
                return 0;
            }
        }
            break;
        default:
            return 0;
            break;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DefrayViewControllerCell" forIndexPath:indexPath];
    cell.imageView.image = nil;
    cell.textLabel.text = nil;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;  //选中不变色
//    UIButton *btn  = [UIButton buttonWithType:UIButtonTypeCustom];
//    [btn setBackgroundColor:[UIColor redColor]];
//    [btn setFrame:CGRectMake(0, 0, 20, 20)];
//    cell.accessoryView = btn;
    UIButton *button = [ UIButton buttonWithType:UIButtonTypeCustom ];
    CGRect frame = CGRectMake( 0.0 , 0.0 , 30,30);
    button. frame = frame;
    [button setBackgroundImage:[UIImage imageNamed:@"666"] forState:UIControlStateNormal ];
    button. backgroundColor = [UIColor clearColor ];
    [button addTarget:self action:@selector(btnClicked: event:) forControlEvents:UIControlEventTouchUpInside ];
    cell. accessoryView = button;
    //cell.accessoryView = self.arrowBtn;
    //cell.accessoryType = UITableViewCellAccessoryCheckmark;
    switch (indexPath.section) {
        case 0:
        {
            if (indexPath.row == self.invoices.count) {
                cell.imageView.image = [UIImage imageNamed:@"add"];
                return cell;
            }
            cell.textLabel.text = self.invoices[indexPath.row];
            return cell;
        }
            break;
            
        case 1:
        {
            if (indexPath.row == self.addresses.count) {
                cell.imageView.image = [UIImage imageNamed:@"add"];
                return cell;
            }
            cell.textLabel.text = self.addresses[indexPath.row];
            return cell;
        }
            break;
        case 2:
        {
            if (indexPath.row == self.accounts.count) {
                cell.imageView.image = [UIImage imageNamed:@"add"];
                return cell;
            }
            cell.textLabel.text = self.accounts[indexPath.row];
            return cell;
        }
            break;
        default:
            return nil;
            break;
    }
}

// cell选中按钮
- (void)btnClicked:(id)sender event:(id)event
{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
    NSArray *array = self.myArray[indexPath.section];
    [self.nameArray replaceObjectAtIndex:indexPath.section withObject:array[indexPath.row]];
    [self.tableView reloadData];
}

#pragma mark - UITableViewDelegate
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HeaderView *headerView = [HeaderView headerViewWithTableView:tableView];
    headerView.addressGroupModel = self.array[section];
    headerView.delegate = self;
    headerView.name = self.nameArray[section];
    return headerView;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 2) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 150)];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 100)];
        label.text = @"需要支付金额￥5000元";
        label.font = [UIFont systemFontOfSize:20];
        label.textAlignment = NSTextAlignmentCenter;
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 50);
        [btn addTarget:self action:@selector(btnClick1) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:@"确认支付" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        //label.backgroundColor = [UIColor redColor];
        btn.backgroundColor = [UIColor blackColor];
        [view addSubview:label];
        [view addSubview:btn];
        return view;
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 2) {
        return 150;
    }
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *vc = [[UIViewController alloc]init];
    vc.view.backgroundColor = [UIColor redColor];
    switch (indexPath.section) {
        case 0:
        {
            if (indexPath.row == self.invoices.count) {
                [self.navigationController pushViewController:vc animated:YES];
            }
        }
            break;
            
        case 1:
        {
            if (indexPath.row == self.addresses.count) {
                  [self.navigationController pushViewController:vc animated:YES];
            }

        }
            break;
        case 2:
        {
            if (indexPath.row == self.accounts.count) {
                  [self.navigationController pushViewController:vc animated:YES];
            }

        }
            break;
        default:
            break;
    }

}


#pragma maek - HeaderDelegate
-(void)headerViewDidClickHeaderView:(HeaderView *)headerView
{
    [self.tableView reloadData];
}

#pragma mark - 确认支付按钮监听
-(void)btnClick1
{
    DefraySucceedViewController *vc = [DefraySucceedViewController initWithStoryBoard];
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - 假数据懒加载
- (NSArray *)addresses{
    if (_addresses == nil) {
        _addresses = [[NSArray alloc]initWithObjects:@"山东省青岛市",@"山东省临沂市",@"山东省济南市", nil];
    }
    return _addresses;
}

- (NSArray *)invoices
{
    if (_invoices == nil) {
        _invoices = [[NSArray alloc]initWithObjects:@"公司A",@"公司B",@"公司C",@"公司D",@"公司E", nil];
    }
    return _invoices;
}

- (NSArray *)accounts
{
    if (_accounts == nil) {
        _accounts = [[NSArray alloc]initWithObjects:@"绑定账户",@"对公账户", nil];
    }
    return _accounts;
}

- (NSArray *)array
{
    if (_array == nil) {
        AddressGroupModel *addressGroupModel0 = [[AddressGroupModel alloc]init];
        addressGroupModel0.addresses = self.invoices;
        AddressGroupModel *addressGroupModel1 = [[AddressGroupModel alloc]init];
        addressGroupModel1.addresses = self.addresses;
        AddressGroupModel *addressGroupModel2 = [[AddressGroupModel alloc]init];
        addressGroupModel2.addresses = self.accounts;
        self.array  = [[NSArray alloc]initWithObjects:addressGroupModel0,addressGroupModel1,addressGroupModel2, nil];
    }
    return _array;
}

- (UIButton *)accessoryBtn
{
    if (_accessoryBtn == nil) {
        _accessoryBtn = [UIButton buttonWithType:UIButtonTypeCustom];
       // [_accessoryBtn setBackgroundColor:[UIColor redColor]];
        //[_accessoryBtn setFrame:CGRectMake(0, 0, 20, 20)];
    }
    return _accessoryBtn;
}

- (UIView *)xxview
{
    if (_xxview == nil) {
        _xxview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 33, 33)];
        _xxview.backgroundColor = [UIColor redColor];
    }
    return _xxview;
}
-(UIImageView *)arrowBtn
{
    if (_arrowBtn == nil) {
        _arrowBtn = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"CellArrow"]];
    }
    return _arrowBtn;
}
- (NSMutableArray *)nameArray
{
    if (_nameArray == nil) {
        _nameArray = [[NSMutableArray alloc]initWithObjects:@"请选择",@"请选择",@"请选择", nil];
    }
    return _nameArray;
}
- (NSArray *)myArray
{
    if (_myArray == nil) {
        _myArray = [[NSArray alloc] initWithObjects:self.invoices,self.addresses,self.accounts,nil];
    }
    return _myArray;
}
@end
